package com.ruoyi.cderp.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cderp.mapper.MaterialMapper;
import com.ruoyi.cderp.domain.Material;
import com.ruoyi.cderp.service.IMaterialService;

/**
 * 物料Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-05
 */
@Service
public class MaterialServiceImpl implements IMaterialService
{
    @Autowired
    private MaterialMapper materialMapper;

    LoginUser loginUser = null;

    /**
     * 查询物料
     *
     * @param id 物料主键
     * @return 物料
     */
    @Override
    public Material selectMaterialById(String id)
    {
        return materialMapper.selectMaterialById(id);
    }

    /**
     * 查询物料列表
     *
     * @param material 物料
     * @return 物料
     */
    @Override
    public List<Material> selectMaterialList(Material material)
    {
        return materialMapper.selectMaterialList(material);
    }

    /**
     * 新增物料
     *
     * @param material 物料
     * @return 结果
     */
    @Override
    public int insertMaterial(Material material)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String formattedDate = sdf.format(new Date());
        Random random = new Random();
        int randomNumber = 10000000 + random.nextInt(90000000); // 生成8位数随机数
        material.setId("18" + formattedDate+randomNumber);
        material.setCreateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        material.setCreateBy(loginUser.getUser().getUserName());
        material.setCreateTime(DateUtils.getNowDate());
        return materialMapper.insertMaterial(material);
    }

    /**
     * 修改物料
     *
     * @param material 物料
     * @return 结果
     */
    @Override
    public int updateMaterial(Material material)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        material.setUpdateBy(loginUser.getUser().getUserName());
        material.setUpdateTime(DateUtils.getNowDate());
        return materialMapper.updateMaterial(material);
    }
    /**
     * 批量删除物料
     *
     * @param ids 需要删除的物料主键
     * @return 结果
     */
    @Override
    public int deleteMaterialByIds(String[] ids)
    {
        return materialMapper.deleteMaterialByIds(ids);
    }

    /**
     * 删除物料信息
     *
     * @param id 物料主键
     * @return 结果
     */
    @Override
    public int deleteMaterialById(String id)
    {
        return materialMapper.deleteMaterialById(id);
    }
}
